﻿/*
 * Description: 热表批次入库处理服务
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * create Date:2024.11.12
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections;
using DatabaseAccess.DBUtility;
using Newtonsoft.Json;
using System.Data;
using log4net;

namespace SendIndustrialNetwork.Service
{
    class RbContainerService
    {
        public string rbFactoryID = ConfigurationManager.AppSettings["RBFactoryID"].ToString();//热表分厂ID
        ILog rsLog = LogHelp.receiveSave;
        Dictionary<string, object> model = new Dictionary<string, object>();
        public RbContainerService(Dictionary<string, object> _model)
        {
            model = _model;
        }

        /// <summary>
        /// 数据处理（唯一对外方法）
        /// </summary>
        public void Excute()
        {
            switch (model["tablename"].ToString().ToLower())
            {
                case "containercurrentinfo": //处理批次
                    ExcuteContainer();
                    break;
                case "checkqtychangerecord": //处理检验数量变更
                    ExcuteCheckChange();
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 热表批次统一处理方法
        /// </summary>
        /// <param name="model"></param>
        private void ExcuteContainer()
        {
            //foreach (Dictionary<string, string> item in model.Items)
            //{
            //    if (item.ContainsKey("isinstore".ToUpper()) && item["isinstore".ToUpper()] == "1") //如果批次的入库字段是1，则表示入库
            //    {
            //        ContainerInstore(item["containerid".ToUpper()]);//处理批次入库
            //    }
            //    //后续可以做其他操作
            //}
        }

        /// <summary>
        /// 热表检验报废统一处理方法（只需处理批次的子序号即可）
        /// </summary>
        /// <param name="model"></param>
        private void ExcuteCheckChange()
        {
            ArrayList sqlList = new ArrayList();
            List<Dictionary<string, string>> items = (List<Dictionary<string, string>>)model["items"];
            foreach (Dictionary<string, string> item in items)
            {
                sqlList.Add(string.Format("UPDATE container c SET c.status = 2,c.qty = 0 WHERE c.containerid = '{0}' AND c.parentcontainerid IS NOT NULL", item["containerid".ToUpper()]));
            }
            DatabaseHelper.ExecuteSqlTran(sqlList);
        }

        /// <summary>
        /// 批次入库
        /// </summary>
        /// <param name="containerID"></param>
        private void ContainerInstore(string containerID)
        {
            string str = string.Format("{ action: \"SaveContainerInstore\",containerid:\"{0}\" }", containerID);
            string res = HttpUtils.Post("http://localhost/InSiteWebApplication/Ashx/ReBiaoConainer/RbContainerInstoreManageHandler.ashx", str);
            uMESProcessResult result = JsonConvert.DeserializeObject<uMESProcessResult>(res);
            rsLog.Info(result.Message);
        }

        public class uMESProcessResult
        {
            public uMESProcessResult() { }
            public string Code { get; set; }
            public DataSet DBDataSet { get; set; }
            public DataTable DBTable { get; set; }
            public string Message { get; set; }
            public string PageCount { get; set; }
            public bool Result { get; set; }
            public object ReturnData { get; set; }
            public string RowCount { get; set; }
        }
    }
}
